{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "618f9504-58ff-43e5-be94-c10bcbde8490",
   "metadata": {},
   "source": [
    "### DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adddf3a4-e209-44c2-88ec-a8170ce3135e",
   "metadata": {},
   "source": [
    "##### · 通过二维数组创建DataFrame\n",
    "##### · 通过字典的方式创建DataFrame\n",
    "##### · 索引对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54ce6795-b5ec-4800-85a1-767d437717e9",
   "metadata": {},
   "source": [
    "### 通过二维数组创建DatFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d27c4061-3ff6-4dd4-a63a-87816db77706",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas import Series, DataFrame\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9b525897-897c-4c7f-9d86-7b207ada2506",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Tom</td>\n",
       "      <td>Merry</td>\n",
       "      <td>John</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>76</td>\n",
       "      <td>98</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     0      1     2\n",
       "0  Tom  Merry  John\n",
       "1   76     98   100"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df01 = DataFrame([['Tom','Merry','John'], [76,98,100]])\n",
    "df01\n",
    "# 行索引index   为 0 1\n",
    "# 列索引columns 为 0 1 2\n",
    "# 数据值为 value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "83c6b0ba-b61c-4eff-a149-6f3cea9efa7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Tom</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Merry</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>John</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       0    1\n",
       "0    Tom   76\n",
       "1  Merry   98\n",
       "2   John  100"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df02 = DataFrame([['Tom',76],['Merry',98],['John',100]])\n",
    "df02\n",
    "# 行索引index   为 0 1 2\n",
    "# 列索引columns 为 0 1 \n",
    "# 数据值为 value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4db6033e-2a02-4df6-bd35-8dcb0e7ff8e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>第一行</th>\n",
       "      <td>Tom</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>第二行</th>\n",
       "      <td>Merry</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>第三行</th>\n",
       "      <td>John</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      name score\n",
       "第一行    Tom    76\n",
       "第二行  Merry    98\n",
       "第三行   John   100"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([['Tom',76],['Merry',98],['John',100]])\n",
    "df03 = DataFrame(arr, columns=['name','score'],index=['第一行','第二行','第三行'])\n",
    "# 重定义 列 名称   行 名称\n",
    "df03"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfaf3bc7-5cd7-4dfb-8d60-bf6fbcd5a172",
   "metadata": {},
   "source": [
    "### 通过字典的方式创建DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ec23fe0c-ad6d-45ae-8cc5-d267c987c122",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>apart</th>\n",
       "      <th>profits</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1001</td>\n",
       "      <td>567.78</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1002</td>\n",
       "      <td>987.87</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1003</td>\n",
       "      <td>873.00</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1001</td>\n",
       "      <td>498.87</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  apart  profits  year\n",
       "0  1001   567.78  2001\n",
       "1  1002   987.87  2001\n",
       "2  1003   873.00  2001\n",
       "3  1001   498.87  2000"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {\"apart\":['1001','1002','1003','1001'],\n",
    "        \"profits\":[567.78,987.87,873,498.87],\n",
    "        \"year\":[2001,2001,2001,2000]}\n",
    "df = DataFrame(data)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "e7216acb-f739-40a6-8058-b7aa503fea22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=4, step=1)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index # 若index没有重定义，就会显示以下信息，起始索引，总项数，步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "bbe87f2c-c9f8-4ac9-8c9e-b1252fccf2a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['apart', 'profits', 'year'], dtype='object')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "c8357f31-b427-461b-bf9b-25ff67c81c85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['1001', 567.78, 2001],\n",
       "       ['1002', 987.87, 2001],\n",
       "       ['1003', 873.0, 2001],\n",
       "       ['1001', 498.87, 2000]], dtype=object)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "8d09b74b-1df2-47bd-a6c9-a9a2efb75dc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>apart</th>\n",
       "      <th>profits</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>1001</td>\n",
       "      <td>567.78</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>1002</td>\n",
       "      <td>987.87</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>1003</td>\n",
       "      <td>873.00</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>four</th>\n",
       "      <td>1001</td>\n",
       "      <td>498.87</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      apart  profits  year\n",
       "one    1001   567.78  2001\n",
       "two    1002   987.87  2001\n",
       "three  1003   873.00  2001\n",
       "four   1001   498.87  2000"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {\"apart\":['1001','1002','1003','1001'],\n",
    "        \"profits\":[567.78,987.87,873,498.87],\n",
    "        \"year\":[2001,2001,2001,2000]}\n",
    "df = DataFrame(data,index=['one','two','three','four'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "2b569efc-9143-4c65-9d45-78bf8b57ad0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['one', 'two', 'three', 'four'], dtype='object')"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index # 若index重定义了，就会显示名称信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46eb57dd-1c21-448f-a2c4-aca196e02862",
   "metadata": {},
   "source": [
    "### 索引对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f61932f-3bc7-46bc-af8b-10a9f37401a7",
   "metadata": {},
   "source": [
    "##### · 不管是Series对象还是DataFrame对象，都有索引对象\n",
    "##### · 索引对象负责管理轴标签和其他元数据\n",
    "##### · 通过索引可以从Series、DataFrame中取值或对某个位置的值重新赋值\n",
    "##### · Series或者DataFrame自动化对齐功能就是通过索引进行的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ca5369e-c3ba-4b43-8c9f-9d9805cf3e5a",
   "metadata": {},
   "source": [
    "#### 通过索引从Series中取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "791b0015-9b94-4b02-8149-d1d700254719",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2001    34.56\n",
       "2002    23.45\n",
       "2003    23.66\n",
       "2004    98.09\n",
       "dtype: float64"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series02 = Series([34.56,23.45,23.66,98.09],\n",
    "                  index=['2001','2002','2003','2004'])\n",
    "series02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "8c7c164b-de6c-4d79-8c67-d1aa703e987d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "23.66"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series02['2003']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "342dd9c3-7b0c-45d7-a91f-909b6f31e095",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2002    23.45\n",
       "2003    23.66\n",
       "2004    98.09\n",
       "dtype: float64"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series02['2002':'2004'] # 边界，右边是包含的，这与Python基础中的列表等不一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "74827d04-9b94-4f8a-8bc9-f780eae53507",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2001    34.56\n",
       "2002    23.45\n",
       "2003    23.66\n",
       "2004    98.09\n",
       "dtype: float64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series02['2001':]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "0227c57f-04ad-45ba-9577-da834b7ebd4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2001    34.56\n",
       "2002    23.45\n",
       "2003    23.66\n",
       "dtype: float64"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series02[:'2003']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "13cc4f5d-9e08-4cda-bfbc-d3394c200120",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2001    35.65\n",
       "2002    23.45\n",
       "2003    23.66\n",
       "2004    98.09\n",
       "dtype: float64"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改某个值\n",
    "series02['2001'] = 35.65\n",
    "series02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "8cf86583-9352-4d89-9d0e-217a33e3d48e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2001    23.45\n",
       "2002    56.78\n",
       "2003    23.66\n",
       "2004    98.09\n",
       "dtype: float64"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series02[:'2002'] = [23.45, 56.78]\n",
    "series02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b478eb50-1495-4a94-acd1-96edcf84b466",
   "metadata": {},
   "source": [
    "#### 通过索引从DataFrame中取值\n",
    "##### · 可以直接通过列索引获取指定列的数据\n",
    "##### · 要通过行索引获取指定行数据需要.iloc方法(.ix方法已被弃用)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "7991a85f-3dc4-4c3d-a468-3350f0edda3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>apart</th>\n",
       "      <th>profits</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>1001</td>\n",
       "      <td>567.78</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>1002</td>\n",
       "      <td>987.87</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>1003</td>\n",
       "      <td>873.00</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>four</th>\n",
       "      <td>1001</td>\n",
       "      <td>498.87</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      apart  profits  year\n",
       "one    1001   567.78  2001\n",
       "two    1002   987.87  2001\n",
       "three  1003   873.00  2001\n",
       "four   1001   498.87  2000"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "94f8b9f5-80ac-4557-8a68-974d03affaee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one      2001\n",
       "two      2001\n",
       "three    2001\n",
       "four     2000\n",
       "Name: year, dtype: int64"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['year']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "97a2bd58-586a-4b9d-8c75-a3fc1b1fe62b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "apart        1001\n",
       "profits    567.78\n",
       "year         2001\n",
       "Name: one, dtype: object"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.ix[0] 弃用了\n",
    "df.iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "1cda6feb-ef6a-4d25-ba52-96aef03b5421",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>apart</th>\n",
       "      <th>profits</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1001</td>\n",
       "      <td>567.78</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1002</td>\n",
       "      <td>987.87</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1003</td>\n",
       "      <td>873.00</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1001</td>\n",
       "      <td>498.87</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  apart  profits  year\n",
       "0  1001   567.78  2001\n",
       "1  1002   987.87  2001\n",
       "2  1003   873.00  2001\n",
       "3  1001   498.87  2000"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = DataFrame(data)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "258ce846-4688-47f3-be72-a214d21a9ab0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>apart</th>\n",
       "      <th>profits</th>\n",
       "      <th>year</th>\n",
       "      <th>pdh</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1001</td>\n",
       "      <td>567.78</td>\n",
       "      <td>2001</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1002</td>\n",
       "      <td>987.87</td>\n",
       "      <td>2001</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1003</td>\n",
       "      <td>873.00</td>\n",
       "      <td>2001</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1001</td>\n",
       "      <td>498.87</td>\n",
       "      <td>2000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  apart  profits  year  pdh\n",
       "0  1001   567.78  2001  NaN\n",
       "1  1002   987.87  2001  NaN\n",
       "2  1003   873.00  2001  NaN\n",
       "3  1001   498.87  2000  NaN"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给DataFrame添加新列 !!注意是用 np.NaN\n",
    "df['pdh'] = np.NAN\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c595562-420a-45c3-b362-22a6bd82eea7",
   "metadata": {},
   "source": [
    "### 唯一值、值计数以及成员资格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "370cd067-fc2f-49e6-becd-0b607ab4dab1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "2    c\n",
       "3    a\n",
       "4    a\n",
       "5    b\n",
       "6    c\n",
       "dtype: object"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser = Series(['a','b','c','a','a','b','c'])\n",
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "db1b528c-6077-46dd-8a61-03c25f03de7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['a', 'b', 'c'], dtype=object)"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "670cfdaf-d0d9-4b8e-9694-8c4dba91fac2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>orderId</th>\n",
       "      <th>orderAmt</th>\n",
       "      <th>memberId</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1001</td>\n",
       "      <td>345.60</td>\n",
       "      <td>a1001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1002</td>\n",
       "      <td>34.23</td>\n",
       "      <td>b1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1003</td>\n",
       "      <td>456.77</td>\n",
       "      <td>a1001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1004</td>\n",
       "      <td>334.55</td>\n",
       "      <td>a1001</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  orderId  orderAmt memberId\n",
       "0    1001    345.60    a1001\n",
       "1    1002     34.23    b1002\n",
       "2    1003    456.77    a1001\n",
       "3    1004    334.55    a1001"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = DataFrame({'orderId':['1001','1002','1003','1004'],\n",
    "                'orderAmt':[345.6,34.23,456.77,334.55],\n",
    "                'memberId':['a1001','b1002','a1001','a1001']})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "caec2cba-3ede-46d3-86e7-99cbfd77a730",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['a1001', 'b1002'], dtype=object)"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['memberId'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "5e1c6bdc-d6b2-4e5b-8cd6-3a6045dfb127",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "2    c\n",
       "3    a\n",
       "4    a\n",
       "5    b\n",
       "6    c\n",
       "dtype: object"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "3dceeba0-e661-4187-a77c-120449fafcb3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    3\n",
       "b    2\n",
       "c    2\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.value_counts() # 值出现的次数统计，默认按出现频率排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "30217f27-18c8-4f62-8035-76942c37c0e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    3\n",
       "b    2\n",
       "c    2\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser.value_counts(ascending=False) # ascending=False（默认）：降序排列，高频在前\n",
    "                                  # ascending=True：升序排列，低频在前"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "6dcf7016-d978-4d33-aa2d-01aea2f8dcb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1     True\n",
       "2     True\n",
       "3    False\n",
       "4    False\n",
       "5     True\n",
       "6     True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = ser.isin(['b','c']) ## 找出全部值中是b和c的值，是的话显示True，否的话显示False\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "9ede7660-6c23-4b74-af18-097229462991",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    b\n",
       "2    c\n",
       "5    b\n",
       "6    c\n",
       "dtype: object"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser[mask] ### 选出值为 b、c 的项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55f5e355-1436-4120-914f-5cdba99126ae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
